<?php
/**
 * 状态模式
 * This file is part of pattern.
 * Author: tangzhou
 * Date: 2016/9/22
 * Time: 14:01
 * File: State.php
 */

interface IIsate
{
    public function WriteCode(Work $w);
}

class AmWork implements IIsate
{
    public function WriteCode(Work $w)
    {
        if ($w->hour <= 12) {
            echo "上午";
        } else {
            $pm = new PmWork();
            $w->setState($pm);
            $w->WriteCode();
        }

    }
}

class PmWork implements IIsate
{
    public function WriteCode(Work $w)
    {
        if ($w->hour <= 17) {
            echo '下午';
        }

    }
}

class Work
{
    public $current;
    public $hour;

    function __construct()
    {
        $this->current =  new AmWork();
    }

    public function WriteCode()
    {
        $this->current->WriteCode($this);
    }

    public function setState(IIsate $state)
    {
        $this->current = $state;
    }
}



$w = new Work();
$w->hour=12;
$w->WriteCode();

$w->hour = 15;
$w->WriteCode();



